<?php


use App\Library\Utils\Arr;
use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;

class UserSeeder extends AbstractSeed
{
    public function getDependencies(): array
    {
        return [
            'AccountSeeder',
            'AreaSeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $faker = FakerFactory::create('zh_CN');

        $accounts = $this->fetchAll('SELECT * FROM kg_account');

        $users = $this->fetchAll('SELECT * FROM kg_user');
        $userIds = array_map(function ($user) {
            return $user['id'];
        }, $users);

        $address = $this->getAddress();

        $rows = [];
        foreach ($accounts as $account) {
            if (in_array($account['id'], $userIds)) {
                continue;
            }
            $rows[] = [
                'id' => $account['id'],
                'name' => $faker->name,
                'avatar' => $faker->imageUrl(96, 96),
                'title' => "演示人员（{$account['id']}）",
                'about' => $faker->text(50),
                'area' => Arr::rand($address),
                'gender' => Arr::rand([1, 2, 3]),
                'vip' => 0,
                'locked' => 0,
                'edu_role' => $faker->randomNumber() % 23 === 0 ? 2 : 1,
                'admin_role' => 0,
                'course_count' => 0,
                'article_count' => 0,
                'question_count' => 0,
                'answer_count' => 0,
                'comment_count' => 0,
                'favorite_count' => 0,
                'report_count' => 0,
                'vip_expiry_time' => 0,
                'lock_expiry_time' => 0,
                'active_time' => $faker->unixTime,
                'create_time' => $faker->unixTime,
                'update_time' => $faker->unixTime,
            ];
        }
        $this->table('kg_user')->insert($rows)->saveData();
    }

    protected function getAddress(): array
    {
        $areas1 = $this->fetchAll('SELECT * FROM kg_area WHERE type=1');
        $areas2 = $this->fetchAll('SELECT * FROM kg_area WHERE type=2');
        $areas3 = $this->fetchAll('SELECT * FROM kg_area WHERE type=3');

        $addr = [];
        foreach ($areas3 as $a3) {
            $code3 = $a3['code'];
            $code2 = substr($code3, 0, 4);
            $code1 = substr($code3, 0, 2);

            $a2 = $this->getAreaByCode($areas2, $code2);
            $a1 = $this->getAreaByCode($areas1, $code1);

            $addr[] = $a1['name'].'/'.$a2['name'].'/'.$a3['name'];
        }
        return $addr;
    }

    protected function getAreaByCode(array $areas, string $code)
    {
        foreach ($areas as $area) {
            if ($area['code'] === $code) {
                return $area;
            }
        }
        return null;
    }
}
